package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.StoreInfoDao;
import cn.topevery.garden.dal.dao.StoreInoutDetailDao;
import cn.topevery.garden.entity.po.StoreInfo;
import cn.topevery.garden.entity.po.StoreInoutDetail;
import cn.topevery.garden.entity.query.StoreInoutDetailQuery;
import cn.topevery.garden.entity.vo.StoreInoutDetailVO;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-30
 */
@Service
public class StoreInoutDetailLogic extends BaseLogic<StoreInoutDetail> {
    @Autowired
    StoreInoutDetailDao storeInoutDetailDao;

    @Autowired
    StoreInfoDao storeInfoDao;

    @Override
    public BaseEntityDao<StoreInoutDetail, Integer> getDao() {
        return storeInoutDetailDao;
    }

    

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<StoreInoutDetail> listPage(StoreInoutDetailQuery query) {
        return storeInoutDetailDao.listPage(query);
    }

    /**
     * 描述 获取盘点清单
     *
     * @param name the name
     * @param type the type
     * @return the list by name
     * @author wenzhao.lin
     * @created 2021 -09-13 15:54:38
     */
    public List<StoreInoutDetailVO> getListByName(String name, Integer type){
        return storeInoutDetailDao.getListByName(name, type);
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        storeInoutDetailDao.delete(userId, ids);
    }

    /**
     * 描述 检查库存数量.
     *
     * @param storeInoutDetailList the store inout detail list
     * @return the json result
     * @author wenzhao.lin
     * @created 2021 -08-30 13:58:15 Check num json result.
     */
    public Map checkNum(List<StoreInoutDetail> storeInoutDetailList){
        boolean isNum  = true;
        String tempName = "";
        int tempNum = 0;
        for(StoreInoutDetail storeInoutDetail : storeInoutDetailList){
            List<StoreInfo> list = storeInfoDao.getStoreList("c_name", storeInoutDetail.getStoreName());
            if(list != null && list.size() >0){
                StoreInfo storeInfo = list.get(0);
                if (storeInfo.getNum()<storeInoutDetail.getNum()){
                    tempNum = storeInoutDetail.getNum();
                    tempName = storeInoutDetail.getStoreName();
                    isNum = false;
                    break;
                }
            }
        }
        Map temp = new HashMap();
        temp.put("status", isNum);
        if(!isNum){
            temp.put("msg", "库存中‘" + tempName + "’这个类型的库存数量不足" + tempNum);
        }
        return temp;

    }

    /**
     * 根据自增id保存或者更新
     *
     * @param userId               the user id
     * @param pid                  the pid
     * @param type                 the type
     * @param storeInoutDetailList 实体
     * @author wenzhao.lin
     * @created 2021 -08-30 11:10:37 Save or update.
     */
    public void saveOrUpdate(String userId,Integer pid,Integer type, List<StoreInoutDetail> storeInoutDetailList){
        for(StoreInoutDetail storeInoutDetail : storeInoutDetailList){
            storeInoutDetail.setType(type);
            storeInoutDetail.setInoutId(pid);
            storeInoutDetailDao.saveOrUpdate(userId,storeInoutDetail);
            updateInfo(userId, storeInoutDetail);
        }
    }

    /**
     * 描述 更新库存表数据.
     *
     * @param userId           the user id
     * @param storeInoutDetail the store inout detail
     * @author wenzhao.lin
     * @created 2021 -08-30 13:36:04 Update info.
     */
    public void updateInfo(String userId, StoreInoutDetail storeInoutDetail){
        List<StoreInfo> list = storeInfoDao.getStoreList("c_name", storeInoutDetail.getStoreName());
        StoreInfo storeInfo = new StoreInfo();
        if(list != null && list.size()>0){
            storeInfo = list.get(0);
            Integer tempNum = 0;

            // type:1-入库， 2-出库
            if(storeInoutDetail.getType() == 1){
                tempNum = storeInfo.getNum() + storeInoutDetail.getNum();
            }else {
                tempNum = storeInfo.getNum() - storeInoutDetail.getNum();
            }

            storeInfo.setNum(tempNum);
        }else {
            storeInfo.setName(storeInoutDetail.getStoreName());
            storeInfo.setTypeName(storeInoutDetail.getStoreTypeName());
            storeInfo.setUnit(storeInoutDetail.getStoreUnit());
            storeInfo.setNum(storeInoutDetail.getNum());
            storeInfo.setType(storeInoutDetail.getType());
        }
        storeInfoDao.saveOrUpdate(userId, storeInfo);
    }
}
